package cn.edu.zjicm.wordsnet_d.fragment;

import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.ViewFlipper;
import cn.edu.zjicm.wordsnet_d.R;
import cn.edu.zjicm.wordsnet_d.activity.ExamRunActivity;
import cn.edu.zjicm.wordsnet_d.data.OneWord;
import cn.edu.zjicm.wordsnet_d.datapersistent.AppPreference;
import cn.edu.zjicm.wordsnet_d.datapersistent.FileManager;
import cn.edu.zjicm.wordsnet_d.media.VoicePlayer;
import cn.edu.zjicm.wordsnet_d.util.TypefaceUtil;

public class ExamRunMode1Fragment extends Fragment implements OnClickListener {

	public ExamRunMode1Fragment(OneWord word, ExamRunActivity examRunActivity) {
		this.mOneWord = word;
		this.mExamRunActivity = examRunActivity;
	}

	public ExamRunMode1Fragment() {

	}

	private ExamRunActivity mExamRunActivity;
	// Data
	private OneWord mOneWord;
	// View
	private TextView mWordText;
	private TextView mWordPhonetic;
	// 右上角按钮
	private ImageView mReadButton;
	private TextView mWriteButton;
	//
	private TextView displayButton;
	private Button easyButton, nextButton;
	private ViewFlipper buttonFlipper;
	private ViewFlipper displayFlipper;
	private RelativeLayout playLemma;

	private FragmentManager fragmentManager;
	private FragmentTransaction ft;
	private Fragment wordInnerFragment;

	private boolean hasShowSyllable = false;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		// return null;
		View v = inflater.inflate(R.layout.fragment_exam_run_mode1, null);
		mWordText = (TextView) v.findViewById(R.id.word_detail_word);
		mWordPhonetic = (TextView) v.findViewById(R.id.word_detail_phonetic);
		mReadButton = (ImageView) v.findViewById(R.id.read_button);
		mWriteButton = (TextView) v.findViewById(R.id.write_button);
		displayButton = (TextView) v.findViewById(R.id.test_display_button);
		easyButton = (Button) v.findViewById(R.id.test_easy_button);
		nextButton = (Button) v.findViewById(R.id.test_next_button);
		displayFlipper = (ViewFlipper) v.findViewById(R.id.diplay_flipper);
		buttonFlipper = (ViewFlipper) v.findViewById(R.id.button_flipper);
		playLemma = (RelativeLayout) v.findViewById(R.id.play_sound);

		if (mOneWord != null)
			initView();
		return v;
	}

	public void initViewForNewWord(OneWord word) {
		this.mOneWord = word;
		initView();
	}

	private void initView() {

		mWordText.setText(mOneWord.getLemma());

		TypefaceUtil.getInstance(mExamRunActivity).setTypeface(mWordPhonetic);
		mWordPhonetic.setText(mOneWord.getPhonetic());

		displayButton.setVisibility(View.VISIBLE);
		buttonFlipper.setDisplayedChild(0);
		displayFlipper.setDisplayedChild(0);

		mWriteButton.setVisibility(View.GONE);
		mWriteButton.setOnClickListener(this);

		setReadIcon(true);

		displayFlipper.setOnClickListener(this);
		easyButton.setOnClickListener(this);
		nextButton.setOnClickListener(this);

		fragmentManager = getChildFragmentManager();
		ft = fragmentManager.beginTransaction();
		wordInnerFragment = fragmentManager.findFragmentByTag("mode1"); // 判断这个fragment是否存在

		if (wordInnerFragment == null) {
			wordInnerFragment = new WordInnerFragment(mOneWord, true, false);
			ft.add(R.id.word_inner_fragment, wordInnerFragment, "mode1").commit();
		} else {
			wordInnerFragment = new WordInnerFragment(mOneWord, true, false);
			ft.replace(R.id.word_inner_fragment, wordInnerFragment, "mode1").commit();
		}

		hasShowSyllable = false;
	}

	void setReadIcon(boolean isPronunciate) {
		boolean isVoiceFileExist = FileManager.isVoiceFileExist(getActivity(), mOneWord);

		if (mOneWord.haveAudio() > 0 && isVoiceFileExist) {
			showReadIcon();
			if (isPronunciate && AppPreference.isAutoPronunciation(mExamRunActivity)) {
				VoicePlayer.getInstance(getActivity()).playWithNoCheckVoiceFile(mOneWord);
			}
		} else if (mOneWord.haveAudio() > 0 && !isVoiceFileExist) {
			showRemindDownloadIcon();
		} else {
			removeReadIcon();
		}
	}

	// 显示发音图标
	void showReadIcon() {
		mReadButton.setVisibility(View.VISIBLE);
		mReadButton.setImageResource(R.drawable.play_icon_selector);
		// Drawable
		// nav_up=getResources().getDrawable(R.drawable.play_icon_selector);
		// nav_up.setBounds(0, 1, nav_up.getMinimumWidth(),
		// nav_up.getMinimumHeight());
		// mWordPhonetic.setCompoundDrawables(null, null, nav_up, null);
		playLemma.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				// 播放读音
				VoicePlayer.getInstance(getActivity()).playWithNoCheckVoiceFile(mOneWord);

				if (!hasShowSyllable) {
					mWordText.setText(mOneWord.getSyllablesSpan(getActivity()));
					hasShowSyllable = true;
				}
			}

		});
	}

	// 显示图标，用户单击后提示下载语音包
	void showRemindDownloadIcon() {
		mReadButton.setVisibility(View.VISIBLE);
		mReadButton.setImageResource(R.drawable.play_ico_disable);
		playLemma.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				mExamRunActivity.showDownloadVoiceWindow(handler);
			}

		});
	}

	// 隐藏发音图标
	void removeReadIcon() {
		mReadButton.setVisibility(View.GONE);
		// mWordPhonetic.setCompoundDrawables(null, null, null, null);
	}

	private Handler handler = new Handler() {
		public void handleMessage(android.os.Message msg) {
			switch (msg.what) {
			case 2:
				setReadIcon(false);
				break;
			}
		};
	};

	@Override
	public void onClick(View view) {
		if (view == mWriteButton) {
			mExamRunActivity.displayWriteView();
			mExamRunActivity.showWriteWindow();
		} else if (view == displayFlipper) {
			displayFlipper.setDisplayedChild(1);
			displayButton.setVisibility(View.GONE);
			buttonFlipper.setDisplayedChild(1);
			if (mOneWord.getLemma_type() == 1)
				mWriteButton.setVisibility(View.VISIBLE);
		} else if (view == easyButton) {
			mExamRunActivity.next(false);
		} else if (view == nextButton) {
			mExamRunActivity.next(true);
		}
	}

}